require "test/unit"
class Test_Koans<Test::Unit::TestCase
  def test_a_pattern_is_a_regular_expression
    assert_equal Regexp, /pattern/.class 
  end 
  # 表明正则表达式属于 Regexp类

  def test_a_regexp_can_search_a_string_for_matching_content
    assert_equal "match", "some matching content" [/match/]
  end  
  # 在string中搜寻match

  def test_a_failed_match_returns_nil
    assert_equal nil, "some matching content" [/missing/]
  end
  # 当string中搜索不到关键词时返回为nil

  def test_question_mark_means_optional
    assert_equal "ab", "abbcccddddeeeee"[/ab?/]
    assert_equal "a",  "abbcccddddeeeee"[/az?/]
  # 1中在string中搜寻ab，只重复一次，若修改为[/a.*b/]的话结果为"abb", 这将会匹配最长的以a开头以b结尾的string,称为贪婪匹配
  # 2中在string中搜寻az， 只重复一次，在string中没有z，故只显示"a", 如果string = "aabab" [/a.*?b/]则匹配aab，ab为第二匹配，匹配以a为开头以b结尾的最短的string, 至于后面的ab更短但是却作为第二匹配，这个里面有优先级
  end

  def test_asterrisk_means_zero_or_more
    assert_equal "ab", "abbcccddddeeeee"[/ab?/]
  # ?代表重复0或1次，故为ab
    assert_equal "a",  "abbcccddddeeeee"[/az?/]
  # 在这个里面没有z，所有返回"a"
  end

  def test_plus_means_one_or_more
    assert_equal "bccc", "abbcccddddeeeee"[/bc+/]
  # + 代表重复一次或更多次，故为bccc
  end

  def test_asterisk_means_zero_or_more
    assert_equal "abb", "abbcccddddeeeee"[/ab*/]
    assert_equal "a",  "abbcccddddeeeee"[/az*/]
    assert_equal "",   "abbcccddddeeeee"[/z*/]
  # *代表重复0次或更多次
  end

  def test_the_left_most_match_wins
    assert_equal "a", "abbccc az"[/az*/]
   #虽然里面有az这个选项，但只能作为第二匹配，有优先级
  end

  def test_character_classes_give_options_for_a_character
    animals = ["cat", "bat", "rat", "zat"]
    assert_equal ["cat", "bat", "rat"], animals.select {|a| a[/[cbr]at/]}
   # 传递一个参数进行匹配，匹配任意以cbr中任一个开头，后面接at的词
  end

  def test_slash_d_is_a_shortcut_for_a_digit_character_class
    assert_equal "42", "the number is 42"[/[0123456789]+/]
    assert_equal "42",  "the number is 42"[/\d+/]
   #  都是以数字进行重复
  end

  def test_character_classes_can_include_ranges
    assert_equal "42", "the number is 42"[/[0-9]+/]
  end
 
  def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
    assert_equal " \t\n", "space: \t\n"[/\s+/]
    # \s匹配空格以后的语句 
  end

  
end
